package com.sangfor.ssl.common;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/* loaded from: classes2.dex */
public class AppFrontMonitor {
    private static final String TAG = "AppFrontMonitor";
    private static boolean foreground = true;
    private final Application.ActivityLifecycleCallbacks ACTIVITY_MONITOR;
    private List<Activity> mActivityList;
    private boolean mIsOnFront;
    private boolean mIsinit;
    private List<OnFrontSwithCallBack> onBackgroundCallbacks;
    private List<OnFrontSwithCallBack> onFrontCallbacks;

    /* loaded from: classes2.dex */
    public interface OnFrontSwithCallBack {
        void callback();
    }

    public void init(Application application) {
        if (this.mIsinit) {
            return;
        }
        application.registerActivityLifecycleCallbacks(this.ACTIVITY_MONITOR);
    }

    public Activity getFrontActivity() {
        if (this.mActivityList.size() == 0) {
            return null;
        }
        List<Activity> list = this.mActivityList;
        return list.get(list.size() - 1);
    }

    public boolean isInit() {
        return this.mIsinit;
    }

    public void addFrontCallback(OnFrontSwithCallBack onFrontSwithCallBack) {
        if (this.onFrontCallbacks.contains(onFrontSwithCallBack)) {
            return;
        }
        this.onFrontCallbacks.add(onFrontSwithCallBack);
    }

    public void removeFrontCallback(OnFrontSwithCallBack onFrontSwithCallBack) {
        if (this.onFrontCallbacks.contains(onFrontSwithCallBack)) {
            this.onFrontCallbacks.remove(onFrontSwithCallBack);
        }
    }

    public void addBackGroundCallback(OnFrontSwithCallBack onFrontSwithCallBack) {
        if (this.onBackgroundCallbacks.contains(onFrontSwithCallBack)) {
            return;
        }
        this.onBackgroundCallbacks.add(onFrontSwithCallBack);
    }

    public void removeBackGroundCallback(OnFrontSwithCallBack onFrontSwithCallBack) {
        if (this.onBackgroundCallbacks.contains(onFrontSwithCallBack)) {
            this.onBackgroundCallbacks.remove(onFrontSwithCallBack);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public synchronized void swithOnFront(boolean z) {
        this.mIsOnFront = z;
        int i = 0;
        if (z) {
            while (i < this.onFrontCallbacks.size()) {
                this.onFrontCallbacks.get(i).callback();
                i++;
            }
        } else {
            while (i < this.onBackgroundCallbacks.size()) {
                this.onBackgroundCallbacks.get(i).callback();
                i++;
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean isForeground(Context context) {
        List<ActivityManager.RunningTaskInfo> runningTasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1);
        return !runningTasks.isEmpty() && runningTasks.get(0).topActivity.getPackageName().equals(context.getPackageName());
    }

    public void clearAllCallbacks() {
        this.onFrontCallbacks.clear();
        this.onBackgroundCallbacks.clear();
    }

    private AppFrontMonitor() {
        this.mActivityList = new LinkedList();
        this.ACTIVITY_MONITOR = new Application.ActivityLifecycleCallbacks() { // from class: com.sangfor.ssl.common.AppFrontMonitor.1
            @Override // android.app.Application.ActivityLifecycleCallbacks
            public void onActivityCreated(Activity activity, Bundle bundle) {
            }

            @Override // android.app.Application.ActivityLifecycleCallbacks
            public void onActivityDestroyed(Activity activity) {
            }

            @Override // android.app.Application.ActivityLifecycleCallbacks
            public void onActivityPaused(Activity activity) {
            }

            @Override // android.app.Application.ActivityLifecycleCallbacks
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
            }

            @Override // android.app.Application.ActivityLifecycleCallbacks
            public void onActivityStarted(Activity activity) {
            }

            @Override // android.app.Application.ActivityLifecycleCallbacks
            public void onActivityResumed(Activity activity) {
                if (AppFrontMonitor.foreground) {
                    return;
                }
                boolean unused = AppFrontMonitor.foreground = true;
                AppFrontMonitor.this.swithOnFront(true);
            }

            @Override // android.app.Application.ActivityLifecycleCallbacks
            public void onActivityStopped(Activity activity) {
                if (AppFrontMonitor.this.isForeground(activity)) {
                    return;
                }
                boolean unused = AppFrontMonitor.foreground = false;
                AppFrontMonitor.this.swithOnFront(false);
            }
        };
        this.onFrontCallbacks = new ArrayList();
        this.onBackgroundCallbacks = new ArrayList();
    }

    /* loaded from: classes2.dex */
    private static class SingletonHolder {
        public static final AppFrontMonitor INSTANCE = new AppFrontMonitor();

        private SingletonHolder() {
        }
    }

    public static AppFrontMonitor getInstance() {
        return SingletonHolder.INSTANCE;
    }
}
